﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using SelfInfo.Models;
using System.Data.Entity.Infrastructure;
using System.Data.Entity;
using MVCPager;


namespace SelfInfo.Controllers
{
    public class SelfInfoController : Controller
    {
        private SelfInfoEntities SelfInfo = new SelfInfoEntities();

        public ActionResult Index(int id = 1, int pieceid = 0, string piecename = "" )
        {
            //IList<SELFINFO> sinfo = SelfInfo.SELFINFO.OrderByDescending(c => c.TODAY).ThenByDescending(c => c.ID).ToPagedList<SELFINFO>(id, 10);//.ToList<SELFINFO>();
            var sinfo = SelfInfo.SELFINFO.Where(c=>c.ID!= -1);
            if (pieceid != 0)
            {
                sinfo = sinfo.Where(c => c.PIECEID == pieceid);
            }
            if (piecename != "")
            {
                sinfo = sinfo.Where(c => c.PIECE.NAME.Contains(piecename));
            }
            IList<SELFINFO> sinfoList = sinfo.OrderByDescending(c => c.TODAY).ThenByDescending(c => c.ID).ToList<SELFINFO>();//.ToPagedList<SELFINFO>(id, 10);
            //return View(sinfoList);

            // 分页大小
            int pageSize = 10;
            // 获取分页页码
            if (id == null || id <= 0)
            {
                id = 1;
            }
            IEnumerable<SELFINFO> query = sinfoList.Skip((id - 1) * pageSize).Take(pageSize);
            MVCPagerModel<SELFINFO> model = new MVCPagerModel<SELFINFO>()
            {
                info = query,
                PageIndex = id,
                PageSize = pageSize,
                RecordCount = sinfoList.Count()
            };

            return View(model);

            
        }

        public ActionResult Create()
        {
            SELFINFO si = new SELFINFO();
            si.TODAY = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
            var query = SelfInfo.PIECE.Where(t => t.ISSHOW == true).Select(c => new { c.ID, c.NAME });
            ViewData["PIECEID"] = new SelectList(query.AsEnumerable(), "ID", "NAME");
            var queryType = SelfInfo.TYPE.Select(c => new { c.ID, c.NAME });
            ViewData["PIECETYPE"] = new SelectList(queryType.AsEnumerable(), "ID", "NAME");
            return View(si);
        }

        [HttpPost]
        public ActionResult Create(SELFINFO sinfo)
        {
            if (ModelState.IsValid)
            {
                SelfInfo.SELFINFO.AddObject(sinfo);
                SelfInfo.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(sinfo);

        }

        public ActionResult Edit(int id)
        {

            IQueryable<SELFINFO> si = SelfInfo.SELFINFO
                           .Where(q => q.ID == id);

            return View(si.First<SELFINFO>());
        }

        [HttpPost]
        public ActionResult Edit(SELFINFO sinfo)
        {
            //SELFINFO si = SelfInfo.SELFINFO.First(i => i.ID == sinfo.ID);
            SelfInfo.SELFINFO.Attach(sinfo);
            SelfInfo.ObjectStateManager.ChangeObjectState(sinfo, System.Data.EntityState.Modified);
            
            SelfInfo.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult GetPieceByType(int id =0)
        {

            var query = SelfInfo.PIECE.Where(p => p.TYPE == id).Where(t => t.ISSHOW == null || t.ISSHOW == true).Select(c => new { c.ID, c.NAME });
            ViewData["PIECEID"] = new SelectList(query.AsEnumerable(), "ID", "NAME");
            return PartialView("Partial_Piece");
        }


        public string Delete(int id)
        {
            SELFINFO si = SelfInfo.SELFINFO.First(i => i.ID == id);
            SelfInfo.SELFINFO.Attach(si);
            SelfInfo.ObjectStateManager.ChangeObjectState(si, System.Data.EntityState.Deleted);
            SelfInfo.SaveChanges();
            return "1";
        }
    }
}
